﻿using MediatR;
using Newtonsoft.Json;
using Warehouse.Domain;
using Warehouse.Domain.DTO;
using Warehouse.Infrstructre.Interface;
using Warehouse.Read.API.Applcation.Command.ChartWarehouseCommand;
using static WMSpublic.ParseAPI;
using WMSpublic;

namespace Warehouse.Read.API.Applcation.CommandHandlers.ChartWarehouseCommandHandler
{
    public class ChartStorageCommandHandler : IRequestHandler<ChartStorageCommand, List<ChartDto>>
    {
        //入库产品
        IEnterStoragesInfoRepsitory esRep;
       

        public ChartStorageCommandHandler(IEnterStoragesInfoRepsitory esRep)
        {
            this.esRep = esRep;
        }


        public async Task<List<ChartDto>> Handle(ChartStorageCommand request, CancellationToken cancellationToken)
        {
            //方法一：LINQ 方法语法
            //var a = (await esRep.GetAllAsyns()).ToList();
            //var result = a.GroupBy(sp => sp.AddTime)
            // .Select(group => new
            // {
            //     AddTime = group.Key,
            //     TotalPrice = group.Sum(sp => sp.Sprice * sp.Binlocation)
            // });
            // 方法二：LINQ 查询：

            

            //入库
            var list = (from es in await esRep.GetAllAsyns()
                        group es by es.AddTime into grouped
                        select new ChartDto
                        {
                            AddTime1 = grouped.Key,
                            TotalPrice1 = grouped.Sum(sp => sp.Sprice * sp.Binlocation)
                        }).ToList();
            return list;
        }

        



    }
}
